package MobileCommServer;

import java.io.*;
import java.util.HashMap;

class SenderThread implements Runnable {
	private HashMap<Integer, ClientThread> clients = null;
	private SerialQueue recvQueue = null;
	
	// 전송 스트림
	private DataOutputStream out = null;

	/**
	 * <pre>
	 * @Comment : 생성자
	 * <pre>
	 *
	 * @param server
	 */
	public SenderThread(MobileCommServer server) {
		this.recvQueue = server.getRecvQueue();
		this.clients = server.getClients();
	}
	
	public void run() {
		while(true) {
			try {
				if (this.recvQueue.IsEmpty() == true)
				{
					Thread.yield();
					continue;
				}

				PassData passData = recvQueue.Pop();
				byte[] recvBuf = passData.getBytePassData();
				
				int[] key = new int[1];
				byte[] data = SerialPacket.GetDataPart(recvBuf, key);

				out = (DataOutputStream)clients.get(key[0]).getOut();
				PrintWriter writer = new PrintWriter(out);
				
				String sendData = SerialPacket.ConvertByteArrToStr(data);
				writer.println(sendData);
				writer.flush();

				writer.close();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
